package com.yntsoft.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class DateUtil {
	
	/** 一天时间的毫秒数 */
	public static long ONE_DAY = 3600 * 1000 * 24;
	
	/**
	 * Description:把日期转换成 long
	 * @param time 传入的日期 格式：yyyy-MM-dd HH:mm:ss
	 * @return  long 类型的日期
	 * Copyright　深圳太极软件公司
	 * @author  杨雪令
	 */
	public static long toLong(String time) {

		Date date = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
		try {
			date = sdf.parse(time);
		} catch (ParseException e) {
			try {
				date = sdf2.parse(time);
			} catch (ParseException e2) {
				System.out.println("输入的时间格式不正确哦！");
			}
		}
		if(date == null) return -1;
		return date.getTime();
	}
	
	
	/**
	 * 格式化时间
	 */
	public static String toDate(long time) {

		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		String date = df.format(new Date(time));
		return date;
	}
	
	/**
	 * 格式化时间
	 */
	public static String toTime(long time) {

		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date = df.format(new Date(time));
		return date;
	}
	
	/**
	 * 格式化时间
	 */
	public static String toYear(long time) {

		SimpleDateFormat df = new SimpleDateFormat("yyyy");
		String date = df.format(new Date(time));
		return date;
	}
	
	/**
	 * 格式化时间
	 */
	public static String toMonth(long time) {

		SimpleDateFormat df = new SimpleDateFormat("MM");
		String date = df.format(new Date(time));
		return date;
	}
	
	/**
	 * 格式化时间
	 */
	public static String toDay(long time) {

		SimpleDateFormat df = new SimpleDateFormat("dd");
		String date = df.format(new Date(time));
		return date;
	}
	
	/**
	 * 格式化时间
	 */
	public static String toHour(long time) {

		SimpleDateFormat df = new SimpleDateFormat("HH");
		String date = df.format(new Date(time));
		return date;
	}
	
	
	/**
	 * 格式化时间
	 */
	public static String toMinute(long time) {

		SimpleDateFormat df = new SimpleDateFormat("mm");
		String date = df.format(new Date(time));
		return date;
	}
	
	
	/**
	 * 格式化时间
	 */
	public static String toSecond(long time) {

		SimpleDateFormat df = new SimpleDateFormat("ss");
		String date = df.format(new Date(time));
		return date;
	}
	
	

	/**
	 * 传入一个日期字符串，返回日期带有星期的标识  如：传入2009-11-16 返回 2009年11月16日 星期一
	 * @param date 時間：yyyy-MM-dd
	 */
	public static String toWeek(String date) {

		String week = null;

		String year = date.substring(0, 4);
		String month = date.substring(5, 7);
		String day = date.substring(8, 10);

		Calendar c = Calendar.getInstance();
		SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日 EEE");

		c.set(Integer.parseInt(year), Integer.parseInt(month) - 1, Integer.parseInt(day));

		week = df.format(c.getTime());

		return week;
	}
	
	/**
	 * 格式化时间
	 */
	public static String toSimpleNo(long time) {

		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
		String date = df.format(new Date(time));
		return date;
	}

	/**
	 * 得到今天的时间
	 */
	public static String getToday() {

		String today = "";
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

		today = df.format(new Date());

		return today;
	}

	/**
	 * 得到當前系統时间
	 */
	public static String getTime() {

		String today = "";
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		today = df.format(new Date());

		return today;
	}

	/**
	 * 根据一个日期，得到星期几的日期
	 * @param date 时间(yyyy-MM-dd)
	 * @param ofDay 星期几
	 */
	@SuppressWarnings("static-access")
	public static String getDateOfWeekDay(String date, int ofDay) {

		String year = date.substring(0, 4);
		String month = date.substring(5, 7);
		String day = date.substring(8, 10);

		Calendar c = Calendar.getInstance();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

		c.set(Integer.parseInt(year), Integer.parseInt(month) - 1, Integer.parseInt(day));
		//如果要得到星期天的时间，星期天在国外是一个星期的第一天
		if(ofDay >= 7) ofDay = 0;
		c.set(c.DAY_OF_WEEK, ofDay + 1);

		return df.format(c.getTime());
	}

	
	/**
	 * 得到星期几的日期
	 * @param ofDay 星期几
	 */
	public static String getDateOfWeekDay(int ofDay) {

		String week = null;

		Calendar c = Calendar.getInstance();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

		week = df.format(c.getTime());

		week = getDateOfWeekDay(week, ofDay);

		return week;
	}

	
	/**
	 * 检查日期格式是否为xxxx-xx-xx
	 * @param date
	 * @return
	 */
	public static boolean checkFormat(String date) {

		boolean status = true;

		Calendar c = Calendar.getInstance();
		try {
			String year = date.substring(0, 4);
			String month = date.substring(5, 7);
			String day = date.substring(8, 10);
			c.set(Integer.parseInt(year), Integer.parseInt(month) - 1, Integer.parseInt(day));
		} catch (Exception e) {
			status = false;
		}

		return status;
	}

	/**
	 * 得到一个月以后的日期
	 * @param date 時間
	 */
	public static String getDateOfNextMonth(String date) {

		String year = date.substring(0, 4);
		String month = date.substring(5, 7);
		String day = date.substring(8, 10);

		Calendar c = Calendar.getInstance();

		c.set(Integer.parseInt(year), Integer.parseInt(month), Integer.parseInt(day));

		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		return df.format(c.getTime());
	}
	
	
	/**
	 * 得到一个月以后的日期
	 * @param date 時間
	 * @param ofDay 第幾天
	 */
	public static String getDateInMonth(String date, int ofDay) {

		String year = date.substring(0, 4);
		String month = date.substring(5, 7);

		Calendar c = Calendar.getInstance();

		c.set(Integer.parseInt(year), Integer.parseInt(month)-1, ofDay);

		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

		return df.format(c.getTime());
	}
	
	/**
	 * 得到一个月以后的日期
	 * @param date 時間
	 * @param ofDay 第幾天
	 */
	public static String getDateInMonth(long date, int ofDay) {

		return DateUtil.getDateInMonth(DateUtil.toDate(date), ofDay);
	}
	
	
	
	/**
	 * 获取Select需要的数据
	 * @param yearCount 生成年份的数量
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static Map getSelectData(int yearCount) {

		Map map = new HashMap();
		
		//年份数据
		List yearList = new ArrayList();
		int currentYear = Integer.parseInt(DateUtil.toYear(System.currentTimeMillis()));
		int startYear = currentYear-yearCount+5;
		int endYear = currentYear+yearCount+5;
		if(startYear>currentYear) startYear = currentYear;
		for(int i=startYear; i<endYear; i++){
			if(i<10) yearList.add("0" + i);
			else yearList.add(i);
		}
		
		//月份数据
		List monthList = new ArrayList();
		for(int i=1; i<=12; i++){
			if(i<10) monthList.add("0" + i);
			else monthList.add(i);
		}
		
		//日数据
		List dayList = new ArrayList();
		for(int i=1; i<=31; i++){
			if(i<10) dayList.add("0" + i);
			else dayList.add(i);
		}
		
		//小时数据
		List hourList = new ArrayList();
		for(int i=0; i<24; i++){
			if(i<10) hourList.add("0" + i);
			else hourList.add(i);
		}
		
		//分数据
		List minuteList = new ArrayList();
		for(int i=0; i<60; i++){
			if(i<10) minuteList.add("0" + i);
			else minuteList.add(i);
		}
		
		//秒数据
		List secondList = new ArrayList();
		for(int i=0; i<60; i++){
			if(i<10) secondList.add("0" + i);
			else secondList.add(i);
		}
		
		map.put("yearList", yearList);
		map.put("monthList", monthList);
		map.put("dayList", dayList);
		map.put("hourList", hourList);
		map.put("minuteList", minuteList);
		map.put("secondList", secondList);
		return map;
	}
	
	public static Date strToDate(String str){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");                
		Date date = new Date();
		try {
			date = sdf.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}   
		return date;
	}
	
	/**
	 * 获取今天的开始时间
	 * @return
	 * @author HMK
	 * 2016年8月16日-上午10:12:25
	 */
	public static Long getDateStart(){
		Date date = new Date();
		Long dateTime = date.getTime();
		String dateStr = DateUtil.toDate(dateTime);
		date = DateUtil.strToDate( dateStr+" 00:00:00");
		return date.getTime();
	}
	
	/**
	 * 获取今天的结束时间
	 * @return
	 * @author HMK
	 * 2016年8月16日-上午10:12:25
	 */
	public  static Long getDateEnd(){
		Date date = new Date();
		Long dateTime = date.getTime();
		String dateStr = DateUtil.toDate(dateTime);
		date = DateUtil.strToDate(dateStr+" 23:59:59");
		return date.getTime();
	}
	
	public static String dateToStr(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");                
		return sdf.format(date);
	}
	
	/**
	 * 获取 date 这个日期的周一
	 * @param date
	 * @return
	 * @author HMK
	 * 2016年8月16日-下午1:51:01
	 */
	public static String getWeek(Date date) {//获取周一 日期
		
		Calendar cal = Calendar.getInstance();
		//n为推迟的周数，1本周，-1向前推迟一周，2下周，依次类推
		int n = 0;
		String monday;
		cal.setTime(date);
		cal.add(Calendar.DATE, n*7);
		if(cal.get(Calendar.DAY_OF_WEEK) == 1){//如果是星期天  java 日期是从星期天开始的
			//当前日期向后移动一天 例如 2016-08-21 执行后  2016-08-20
			cal.add(Calendar.DATE,-1);
		}
		//想周几，这里就传几Calendar.MONDAY（TUESDAY...）
		cal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
		monday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
		return monday; 
	}
	
	@SuppressWarnings("deprecation")
	public static Integer getMonthDayNum(Date date) {//获取周一 日期
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR,date.getYear());
		cal.set(Calendar.MONTH, date.getMonth()- 1);//Java月份才0开始算
		int dateOfMonth = cal.getActualMaximum(Calendar.DATE);
		return dateOfMonth; 
	}
	
    /**
     * @return
     * @author HMK
     * 2016年8月16日-下午1:55:12
     */
	//获取当月第一天
	public static String getFirstDayOfMonth(Date date){
		String str = "";
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Calendar lastDate = Calendar.getInstance();
		lastDate.setTime(date);
		lastDate.set(Calendar.DATE,1);//设为当前月的1 号
		str=sdf.format(lastDate.getTime());
		return str;
	}
	
	/**
	 * 測試
	 * @autor yxl
	 */
	public static void main(String[] args) {
//		String day = "2013-08-24";
		//System.out.println(DateUtil.getDateInMonth(day, 1));
		//System.out.println(DateUtil.getDateInMonth(System.currentTimeMillis(), 2));
//		DateUtil.getSelectData(20);
		//
		System.out.println( getWeek(new Date()));
		
		final String dayNames[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五","星期六" };
		Calendar calendar = Calendar.getInstance();
		Date date = new Date();
		calendar.setTime(date);
		
		int dayOfWeek=calendar.get(Calendar.DAY_OF_WEEK)-1;
		if(dayOfWeek<0)
			dayOfWeek=0;
		System.out.println(dayNames[dayOfWeek]);
	}


}
